package farb.top.eurekaclientriskapp.service;

import farb.top.eurekaclientriskapp.model.Risk;
import farb.top.eurekaclientriskapp.repo.RedisRiskRepo;
import farb.top.eurekaclientriskapp.repo.RiskRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RiskService {
    private static final String RISK_KEY_PREFIX = "risk_";
    @Autowired
    private RiskRepo riskRepo;
    @Autowired
    private RedisRiskRepo redisRiskRepo;

    public String getRiskLevel(String userId) {
        Risk risk = redisRiskRepo.getRiskLevel(RISK_KEY_PREFIX + userId);
        if (risk != null) {
            System.out.println("从redis获取数据");
            return risk.getLevel();
        }
        risk = riskRepo.getRiskByUserid(userId);
        System.out.println("从mysql获取数据");
        if (risk != null) {
            redisRiskRepo.saveRiskLevel(RISK_KEY_PREFIX + userId, 60, risk);
            return risk.getLevel();
        }
        return "";
    }
}
